/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    interrupt.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************

  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "myg0025.h"
#include "drv_inc.h"
#include "main.h"

STR_Rttstru                  Rttstru;

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
void NVIC_Configure(uint8_t ch, uint8_t pri)
{
    NVIC_InitTypeDef  NVIC_InitStruct;

    /** Initialization ADC interrupt */
    NVIC_InitStruct.NVIC_IRQChannel = ch;
    NVIC_InitStruct.NVIC_IRQChannelPriority = pri;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
}

/**
 * @brief
 * @param
 * @return
 * @details
 * @date        VersionNumber           Modifier        ModifyContent
 *-------------------------------------------------------------------------------------
 *  2020/6/20         V1.0                 Tim           create
 */
void Interrupt_Init(void)
{
    /** Initialization Systick interrupt */
    NVIC_SetPriority(SysTick_IRQn, SYSTICK_INTERRUPT);

    /** Initialization ADC interrupt */
    NVIC_Configure(ADC_IRQn, ADC1_INTERRUPT);
    REG_ADC_SQUE0_FLAG_CLR();
    ADC_ITConfig(ADC,ADC_IT_EOC_QUE0,ENABLE);

    /** Initialization TIM interrupt */
    NVIC_Configure(TIM1_IRQn, TIM1_INTERRUPT);

    /** TIM Break interrupt disabled */
    TIM_ClearFlag(TIM1, TIM_FLAG_Break);
    TIM_ITConfig(TIM1, TIM_FLAG_Break, ENABLE);
}


/******************************************************************************/
/*           Cortex-M0 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
    /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

    /* USER CODE END NonMaskableInt_IRQn 0 */
    /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

    /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
    /* USER CODE BEGIN HardFault_IRQn 0 */

    /* USER CODE END HardFault_IRQn 0 */
    while (1)
    {
        /* USER CODE BEGIN W1_HardFault_IRQn 0 */
        /* USER CODE END W1_HardFault_IRQn 0 */
    }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
    /* USER CODE BEGIN SVC_IRQn 0 */

    /* USER CODE END SVC_IRQn 0 */
    /* USER CODE BEGIN SVC_IRQn 1 */

    /* USER CODE END SVC_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
    /* USER CODE BEGIN PendSV_IRQn 0 */

    /* USER CODE END PendSV_IRQn 0 */
    /* USER CODE BEGIN PendSV_IRQn 1 */

    /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
    Clear_Over_Flag();
    Inc_Systicks();

    gS_TaskScheduler.bTimeCnt1ms++;
    gS_TaskScheduler.unTimeCnt10ms++;
    gS_TaskScheduler.unTimeCnt500ms++;

    /* todo*/
}

void ADC_IRQHandler(void)
{
    uint16_t u16ADCValue[4];
//    PD4_1;

    //ADC Setting
    REG_ADC_SQUE0_FLAG_CLR();

    u16ADCValue[AIN9] = GET_ADC1_VALUE(9);     //VSP
    u16ADCValue[AIN0] = GET_ADC1_VALUE(0);     //VBUS
    u16ADCValue[AIN12] = GET_ADC1_VALUE(12);     //IU
    u16ADCValue[AIN11] = GET_ADC1_VALUE(11);     //IV

    //receive adc data
    gS_Motor.mS_Ctr.u16ADCIaData = u16ADCValue[0];//xxx rshunt = 0.1ohm, gain = 5.
    gS_Motor.mS_Ctr.u16ADCIbData = u16ADCValue[1];
    gS_Motor.mS_Ctr.unVbus       = u16ADCValue[2];
    gS_VSP.unVspCommand          = u16ADCValue[3];
    gS_Motor.mS_Ctr.unVbusFilter = gS_Motor.mS_Ctr.unVbus;


    //foc main loop
//    FOC_Main_Loop();

//    static STR_TrigComponents   sS_TrigSinCos;
//    sS_TrigSinCos = gS_TrigFunctions(gS_Motor.mS_Ctr.unThetaCmd);

//    FOC_GetPhaseCurrent(&gS_Motor);//rshunt = 0.1ohm, gain = 5. or rshunt = 0.05ohm, gain = 10.
//    FOC_Coordinate_ABCtoDQ(&gS_Motor, &sS_TrigSinCos);
//
//    MotorIPD(&gS_Motor);

//    PD4_0;
//    Rttstru.data1 = gS_Motor.mS_Ctr.unThetaCmd;
//    Rttstru.data2 = gS_Motor.mS_Ctr.u16ADCIaData; //gS_SMO.unPLLTheta;
//    Rttstru.data3 = gS_Motor.mS_Ctr.u16ADCIbData;
//    Rttstru.data4 = gS_IqPI.wUi_15 >> 15;
//    Rttstru.data1 = gS_BEMFOB.unThetaOut;
//    Rttstru.data2 = gS_Motor.mS_Ctr.nId; //gS_SMO.unPLLTheta;
//    Rttstru.data3 = gS_Motor.mS_Ctr.nIq;
//    Rttstru.data4 = gS_BEMFOB.nWr;

#if(P_DEBUG_RTT_EN)
    SEGGER_RTT_Write(1, &Rttstru, 8);
#endif
}

void TIM1_IRQHandler(void)
{
    if (READ_TIM1_BREAK_FLAG())
    {
        CLEAN_TIM1_BREAK_FLAG();
        REG_PWM_Out_Disable();
        gS_Motor.eSYSPROTECT = E_OCP;
    }
}

uint16_t Tim3_INT_CNT = 0;
#if(P_FG_FUNC_EN)
void TIM3_IRQHandler(void)
{
	Tim3_INT_CNT++;
    TIM3->SR &= 0xfffe;
    NVIC_ClearPendingIRQ(TIM3_IRQn);
    FG_IO_PORT->ODR ^= FG_IO_PIN;
}
#endif


/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT Metanergy *****END OF FILE****/
